# -*- coding:utf-8 -*-
'''
Created on 31/05/2011

@author: Hagut
'''
from loa_game import WHITE, MoveAction


INIT_VALUE = 1
BONUS = 2
MALUS = 0.5

class Mobility():
    
    def __init__(self, gameState):
        self.gameState = gameState
        self.board = gameState.board
        self.value = 0
        self.size = gameState.size

    def mobility(self, successors):
        currValue = self.value
        while len(successors) != 0:
            action, state = successors.pop()
            if isinstance(action, MoveAction):
                currValue = INIT_VALUE
                ' check if there was a capture '
                ' does it returns the player that should play right now??? '
                if self.gameState.getCurrentPlayer == WHITE:
                    if self.gameState.blacks > state.blacks:
                        'there was a capture'
                        currValue *= BONUS
                elif self.gameState.whites > state.whites:
                    'there was a capture'
                    currValue *= BONUS
                ' check if going to the edge '
                dir = action.direction
                #print action.row, action.col, dir.delta
                ' first or last row '
                if (((action.row + dir.delta[0]) == 0) or ((action.row + dir.delta[0]) == (self.size-1))):
                    currValue *= MALUS
                ' first or last column '
                if (((action.col + dir.delta[1]) == 0) or ((action.col + dir.delta[1]) == (self.size-1))):
                    currValue *= MALUS
                self.value += currValue
#        print "self.value", self.value
        maxNumOfPieces = 2*self.size-4
        #print "maxNumOfPieces", maxNumOfPieces
        tempValue = float(self.value)/(8.0*maxNumOfPieces)
        #print tempValue
        tempValue -= MALUS*MALUS
        #print tempValue
        self.value = float(tempValue) / (BONUS - MALUS*MALUS)
        return self.value
